define(['app'], function (app) {
    app.controller('BorrowerMngrEditCtrl', ['$scope', '$rootScope', '$timeout', '$http', '$q', 'borrowerMngrService', '$stateParams', 'borrowerService', '$uibModal',
       function ($scope, $rootScope, $timeout, $http, $q, borrowerMngrService, $stateParams, borrowerService, $uibModal) {

           //代码书写步骤
           //1.用【$scope】定义公共类model,和页面绑定的model
           //2.用【var】定义controller内部用的变量
           //3.用【$scope】和【var】定义方法，注意区分两者定义的方法的区别
           //4.最后定义【init】初始化方法，并调用，进行页面的初始化加载
           //定义规则，按可用性分忧优先级，公共的在前，private的在后

           $scope.model = {};
           $scope.id = $stateParams.id;
           $scope.bookModel = {};
           $scope.borrowerModel = {};

           //$scope.getFirst = function (params) {
           //    if (params.total() <= (params.page() - 1) * params.count()) {
           //        return Math.floor(params.total() / params.count()) * params.count() + 1;
           //    }
           //    else {
           //        return (params.page() - 1) * params.count() + 1;
           //    }
           //};

           /**
            * 查询tableList数据
            * @return {[object]} [返回table列表数据]
            */
           //$scope.tableParams = new ngTableParams({ sorting: { 'id': 'desc' } }, {
           //    getData: function (params) {
           //        // $scope.global.isBusy = true;//暂时没有
           //        if ($scope.filter.borrowerName == "") {
           //            $scope.filter.borrowerName = null;
           //        }
           //        return borrowerService.query(params, $scope.filter).then(
           //            function (response) {
           //                //console.log(response);
           //                //// $scope.global.isBusy = false;
           //                //if (response.success) {
           //                $scope.first = $scope.getFirst(params);
           //                var list = response.data.list;
           //                //if (list != null) {
           //                //    for (var i = 0; i < list.length; i++) {
           //                //        list[i].cTime = new Date(list[i].cTime);
           //                //        list[i].uTime = new Date(list[i].uTime);
           //                //    }
           //                //}
           //                return list;
           //                //} else {
           //                //    console.log(response);
           //                //    alert("查询数据失败!");
           //                //}
           //            },
           //            function (code) {
           //                alert("系统出现问题了！");
           //            });
           //    }
           //});

           //$scope.reflash = function () {
           //    $scope.tableParams.page(1);
           //    $scope.tableParams.reload();
           //};

           var Init = function () {
               if ($scope.id == null || $scope.id == undefined || $scope.id == "") {
                   $scope.model = {};
                   $scope.model.gender = 2;
                   $scope.model.credit = 1;
                   return;
               }
               borrowerMngrService.get($scope.id).then(
                     function (response) {
                         if (response.status == 200) {
                             $scope.model = response.rows;

                         } else {
                             console.log(response);
                             alert("查询数据失败!");
                         }
                     },
                     function (code) {
                         alert("系统出现问题了！");
                     });
           };
           $scope.back = function () {
               history.back();
           };

           $scope.save = function () {
               var q = {
                   bookId: $scope.bookModel.id,
                   borrowerId: $scope.borrowerModel.id
               };
               borrowerMngrService.save(q).then(
                     function (response) {
                         //if (response.status == 200) {
                             history.back();
                         //} else {
                         //    console.log(response);
                         //    alert("保存失败!");
                         //}
                     },
                     function (code) {
                         alert("系统出现问题了！");
                     });

           };


           //打开modal框
           $scope.openModel = function () {
               var modalInstance = $uibModal.open({
                   animation: true,
                   templateUrl: 'myModalContent.html',
                   controller: 'selectBorrowerController',
                   size: 'lg',//lg,sm, 默认大小
                   //resolve: {
                   //    items: function () {//页面之间传递参数时使用
                   //        var items = { type: type, item: item };
                   //        return items;
                   //    }
                   //}
               });
               //响应modal关闭时的处理方法
               modalInstance.result.then(function () {
                   //$scope.getCategoryList();
               }, function (obj) {
                   // $log.info('Modal dismissed at: ' + new Date());
                   $scope.borrowerModel = obj;
               });
           };

           //打开modal框
           $scope.openBookModel = function () {
               var modalInstance = $uibModal.open({
                   animation: true,
                   templateUrl: 'myBookModalContent.html',
                   controller: 'selectBookController',
                   size: 'lg',//lg,sm, 默认大小
                   //resolve: {
                   //    items: function () {//页面之间传递参数时使用
                   //        var items = { type: type, item: item };
                   //        return items;
                   //    }
                   //}
               });
               //响应modal关闭时的处理方法
               modalInstance.result.then(function () {
                   //$scope.getCategoryList();
               }, function (obj) {
                   // $log.info('Modal dismissed at: ' + new Date());
                   $scope.bookModel = obj;
               });
           };


           Init();
       }]);
});